<?php
mysql_connect("localhost", "hp_001", "Z9cvEmHwan3GjYy2");
mysql_select_db("hp_001");

$query = "SELECT company_id, address FROM companies";
mysql_query("SET NAMES utf8");
$res = mysql_query($query);

$pattern = "^([0-9]{4})\s(.*?)\s(.*?)\s(u\.|U.|ÚT|U|park|lejtő|UTCA|sgt\.|út\.|stny|völgy|dűlő|DŰLŐ|sétány|tere|útja|ÚTJA|utca|KRT\.|út|tér|köz|körút|krt\.|fasor|sor)\s(.*)$";
$f = fopen("test.txt", "w");

while (($rec = mysql_fetch_assoc($res)) !== false) {
    preg_match("@$pattern@", $rec['address'], $matches);
    $matches[2] = str_replace(",", "", $matches[2]);
    $query = "UPDATE companies SET ".
        "zipcode=" . $matches[1] . ",".
        "city='" . $matches[2] . "',".
        "street='" . $matches[3] . "',".
        "sttype='"  . $matches[4] . "',".
        "houseno='" . $matches[5] . "'".
        " WHERE company_id=" . $rec['company_id'];
    mysql_query($query);
    fputs($f, $rec['address'] . " => " . $matches[1] . "," . $matches[2] . "," . $matches[3] . "," . $matches[4] . "," . $matches[5] . "\r\n");
}
fclose($f);
mysql_close();
echo "Ready";

?>
